# Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
# tree. An additional intellectual property rights grant can be found
# in the file PATENTS.  All contributing project authors may
# be found in the AUTHORS file in the root of the source tree.

import("//build/config/arm.gni")
import("//third_party/protobuf/proto_library.gni")
import("../../build/webrtc.gni")

declare_args() {
  # Outputs some low-level debug files.
  aec_debug_dump = false

  # Disables the usual mode where we trust the reported system delay
  # values the AEC receives. The corresponding define is set appropriately
  # in the code, but it can be force-enabled here for testing.
  aec_untrusted_delay_for_testing = false
}

source_set("audio_processing") {
  sources = [
    "aec/aec_core.c",
    "aec/aec_core.h",
    "aec/aec_core_internal.h",
    "aec/aec_rdft.c",
    "aec/aec_rdft.h",
    "aec/aec_resampler.c",
    "aec/aec_resampler.h",
    "aec/echo_cancellation.c",
    "aec/echo_cancellation_internal.h",
    "aec/include/echo_cancellation.h",
    "aecm/aecm_core.c",
    "aecm/aecm_core.h",
    "aecm/echo_control_mobile.c",
    "aecm/include/echo_control_mobile.h",
    "agc/agc.cc",
    "agc/agc.h",
    "agc/agc_audio_proc.cc",
    "agc/agc_audio_proc.h",
    "agc/agc_audio_proc_internal.h",
    "agc/agc_manager_direct.cc",
    "agc/agc_manager_direct.h",
    "agc/circular_buffer.cc",
    "agc/circular_buffer.h",
    "agc/common.h",
    "agc/gain_map_internal.h",
    "agc/gmm.cc",
    "agc/gmm.h",
    "agc/histogram.cc",
    "agc/histogram.h",
    "agc/legacy/analog_agc.c",
    "agc/legacy/analog_agc.h",
    "agc/legacy/digital_agc.c",
    "agc/legacy/digital_agc.h",
    "agc/legacy/gain_control.h",
    "agc/noise_gmm_tables.h",
    "agc/pitch_based_vad.cc",
    "agc/pitch_based_vad.h",
    "agc/pitch_internal.cc",
    "agc/pitch_internal.h",
    "agc/pole_zero_filter.cc",
    "agc/pole_zero_filter.h",
    "agc/standalone_vad.cc",
    "agc/standalone_vad.h",
    "agc/utility.cc",
    "agc/utility.h",
    "agc/voice_gmm_tables.h",
    "audio_buffer.cc",
    "audio_buffer.h",
    "audio_processing_impl.cc",
    "audio_processing_impl.h",
    "beamformer/beamformer.h",
    "beamformer/complex_matrix.h",
    "beamformer/covariance_matrix_generator.cc",
    "beamformer/covariance_matrix_generator.h",
    "beamformer/matrix.h",
    "beamformer/nonlinear_beamformer.cc",
    "beamformer/nonlinear_beamformer.h",
    "common.h",
    "echo_cancellation_impl.cc",
    "echo_cancellation_impl.h",
    "echo_control_mobile_impl.cc",
    "echo_control_mobile_impl.h",
    "gain_control_impl.cc",
    "gain_control_impl.h",
    "high_pass_filter_impl.cc",
    "high_pass_filter_impl.h",
    "include/audio_processing.h",
    "level_estimator_impl.cc",
    "level_estimator_impl.h",
    "noise_suppression_impl.cc",
    "noise_suppression_impl.h",
    "processing_component.cc",
    "processing_component.h",
    "rms_level.cc",
    "rms_level.h",
    "splitting_filter.cc",
    "splitting_filter.h",
    "transient/common.h",
    "transient/daubechies_8_wavelet_coeffs.h",
    "transient/dyadic_decimator.h",
    "transient/moving_moments.cc",
    "transient/moving_moments.h",
    "transient/transient_detector.cc",
    "transient/transient_detector.h",
    "transient/transient_suppressor.cc",
    "transient/transient_suppressor.h",
    "transient/wpd_node.cc",
    "transient/wpd_node.h",
    "transient/wpd_tree.cc",
    "transient/wpd_tree.h",
    "typing_detection.cc",
    "typing_detection.h",
    "utility/delay_estimator.c",
    "utility/delay_estimator.h",
    "utility/delay_estimator_internal.h",
    "utility/delay_estimator_wrapper.c",
    "utility/delay_estimator_wrapper.h",
    "voice_detection_impl.cc",
    "voice_detection_impl.h",
  ]

  configs += [ "../..:common_config" ]
  public_configs = [ "../..:common_inherited_config" ]

  defines = []
  deps = [ "../..:webrtc_common" ]

  if (aec_debug_dump) {
    defines += [ "WEBRTC_AEC_DEBUG_DUMP" ]
  }

  if (aec_untrusted_delay_for_testing) {
    defines += [ "WEBRTC_UNTRUSTED_DELAY" ]
  }

  if (rtc_enable_protobuf) {
    defines += [ "WEBRTC_AUDIOPROC_DEBUG_DUMP" ]
    deps += [ ":audioproc_debug_proto" ]
  }

  if (rtc_prefer_fixed_point) {
    defines += [ "WEBRTC_NS_FIXED" ]
    sources += [
      "ns/include/noise_suppression_x.h",
      "ns/noise_suppression_x.c",
      "ns/nsx_core.c",
      "ns/nsx_core.h",
      "ns/nsx_defines.h",
    ]
    if (current_cpu == "mipsel") {
      sources += [ "ns/nsx_core_mips.c" ]
    } else {
      sources += [ "ns/nsx_core_c.c" ]
    }
  } else {
    defines += [ "WEBRTC_NS_FLOAT" ]
    sources += [
      "ns/defines.h",
      "ns/include/noise_suppression.h",
      "ns/noise_suppression.c",
      "ns/ns_core.c",
      "ns/ns_core.h",
      "ns/windows_private.h",
    ]
  }

  if (current_cpu == "x86" || current_cpu == "x64") {
    deps += [ ":audio_processing_sse2" ]
  }

  if (rtc_build_armv7_neon || current_cpu == "arm64") {
    deps += [ ":audio_processing_neon" ]
  }

  if (current_cpu == "mipsel") {
    sources += [ "aecm/aecm_core_mips.c" ]
    if (mips_float_abi == "hard") {
      sources += [
        "aec/aec_core_mips.c",
        "aec/aec_rdft_mips.c",
      ]
    }
  } else {
    sources += [ "aecm/aecm_core_c.c" ]
  }

  if (is_win) {
    cflags = [
      # TODO(jschuh): Bug 1348: fix this warning.
      "/wd4267",  # size_t to int truncations
    ]
  }

  if (is_clang) {
    # Suppress warnings from Chrome's Clang plugins.
    # See http://code.google.com/p/webrtc/issues/detail?id=163 for details.
    configs -= [ "//build/config/clang:find_bad_constructs" ]
  }

  deps += [
    "../../base:rtc_base_approved",
    "../../common_audio",
    "../../system_wrappers",
  ]
}

if (rtc_enable_protobuf) {
  proto_library("audioproc_debug_proto") {
    sources = [ "debug.proto" ]

    proto_out_dir = "webrtc/audio_processing"
  }
}

if (current_cpu == "x86" || current_cpu == "x64") {
  source_set("audio_processing_sse2") {
    sources = [
      "aec/aec_core_sse2.c",
      "aec/aec_rdft_sse2.c",
    ]

    cflags = [ "-msse2" ]

    configs += [ "../..:common_config" ]
    public_configs = [ "../..:common_inherited_config" ]
  }
}

if (rtc_build_armv7_neon || current_cpu == "arm64") {
  source_set("audio_processing_neon") {
    sources = [
      "aec/aec_core_neon.c",
      "aec/aec_rdft_neon.c",
      "aecm/aecm_core_neon.c",
      "ns/nsx_core_neon.c",
    ]

    configs += [ "../..:common_config" ]
    public_configs = [ "../..:common_inherited_config" ]

    deps = [ "../../common_audio" ]

    # Enable compilation for the ARM v7 Neon instruction set. This is needed
    # since //build/config/arm.gni only enables Neon for iOS, not Android.
    # This provides the same functionality as webrtc/build/arm_neon.gypi.
    # TODO(kjellander): Investigate if this can be moved into webrtc.gni or
    # //build/config/arm.gni instead, to reduce code duplication.
    # Remove the -mfpu=vfpv3-d16 cflag.
    configs -= [ "//build/config/compiler:compiler_arm_fpu" ]

    # "-mfpu=neon" is not requried for arm64 in GCC.
    if (current_cpu != "arm64") {
       cflags = [ "-mfpu=neon" ]
    }

    # Disable LTO in audio_processing_neon target due to compiler bug.
    if (rtc_use_lto) {
      cflags -= [
        "-flto",
        "-ffat-lto-objects",
      ]
    }
  }
}
